package com.winter.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.PeoplejobsMapper;
import com.winter.mapper.PersonalReleaseMapper;
import com.winter.model.DictEntry;
import com.winter.model.PersonalRelease;
import com.winter.model.vo.check.CheckParam;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class PersonalReleaseService {

    @Autowired
    public PersonalReleaseMapper personalReleaseMapper;

    @Autowired
    public PeoplejobsMapper peoplejobsMapper;

    @Autowired
    private FinFavInfoMapper finFavInfoMapper;
    
    @Autowired
    private DictEntryMapper dictEntryMapper;

    /**
     * 查询职场 高级人才列表
     */
    public Result queryPageList(Integer pageNumber, Integer pageSize, String worklife, String key, String userNo, Integer dcTime, String dcSalary) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        Short open = null;
        if (Strings.isNullOrEmpty(userNo)) {
            open = 1;
        }
        String timeStart = null;
        String timeEnd = null;
        if (dcTime != null) {
            Map<String, Object> map = CompanyPositionService.getTime(dcTime, timeStart, timeEnd);
            timeStart = map.get("timeStart").toString() + " 00:00:00";
            timeEnd = map.get("timeEnd").toString() + " 23:59:59";
        }
        Integer startWorklife = null;
        Integer endWorklife = null;
        Integer startWork = null;
        Integer endWork= null;
        if (!Strings.isNullOrEmpty(worklife)) {
           if (worklife.equals("1")) {
        	   startWork = 1;
           }
           if (worklife.equals("2")) {
        	   startWorklife = 2;
        	   endWorklife = 3;
           }
           if (worklife.equals("3")) {
        	   startWorklife = 4;
        	   endWorklife = 6;
           }
           if (worklife.equals("4")) {
        	   startWorklife = 7;
        	   endWorklife = 10;
           }
           if (worklife.equals("5")) {
        	   endWork = 10;
           }
        }
        String orderBy = "orderBy";
        // 获取总数量
        List<PersonalRelease> list1 = personalReleaseMapper.queryPageList(orderBy, key, null, open, timeStart, timeEnd, dcSalary,startWorklife,endWorklife,startWork,endWork);
        PageHelper.startPage(pageNumber, pageSize, false);
        List<PersonalRelease> list = personalReleaseMapper.queryPageList(orderBy, key, null, open, timeStart, timeEnd, dcSalary,startWorklife,endWorklife,startWork,endWork);
        if (list.size() > 0) {
        	for (int i = 0; i < list.size(); i++) {
            	getTypeName(list.get(i));
            }
		}
        PageInfo<PersonalRelease> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(list1.size());
        return Result.success(pageInfo);
    }

    /**
     * 查询职场 高级人才详情
     */
    public Result queryById(Long id, String userNo, String hirejob, String userType) {
        if (id == null) {
            return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "主键id不能为空");
        }
        List<PersonalRelease> list = personalReleaseMapper.queryById(id, null, hirejob, null);
        PersonalRelease personalRelease = null;
        if (list.size() > 0) {
            personalRelease = list.get(0);
            personalRelease.setIsCollect("0");
            // 判断是否收藏
            Long aLong = finFavInfoMapper.get(personalRelease.getId(), "8", 2, userNo);
            if (aLong > 0) {
                personalRelease.setIsCollect("1");
            }
            if (!Strings.isNullOrEmpty(userType)) {
                if (userType.equals("0") || userType.equals("1") || userType.equals("2")) {
                    personalRelease.setShowName("");
                    personalRelease.setCustMobile("");
                    personalRelease.setCustEmail("");
                }
            }
            getTypeName(personalRelease);
        }
        return Result.success(personalRelease);
    }

    /**
     * 发布简历
     */
    public Result Insert(PersonalRelease record) {
        if (Strings.isNullOrEmpty(record.getUserNo())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        // 校验参数
        String checkPersonal = CheckParam.checkPersonal(record);
        if (checkPersonal != null) {
            return Result.failure(ResultCode.TRUSTED_USER_EXIST, checkPersonal);
        }
        record.setCreateUser(record.getUserNo());
        record.setCreatetime(new Date());
        record.setUpdatetime(new Date());
        record.setUpdateUser(record.getCreateUser());
        record.setOpen("1");
        record.setIsvalid("1");
        record.setAuditing("0");
        record.setArea("1");
        Long i = personalReleaseMapper.insertSelective(record);
        if (i <= 0) {
            return Result.failure(ResultCode.FAILURE, "发布简历失败");
        }
        return Result.success(record);
    }

    /**
     * 选择简历
     */
    public Result queryByKeyId(Long id, String userNo, String hirejob) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户账户不能为空");
        }
        List<PersonalRelease> list = personalReleaseMapper.queryById(null, userNo, hirejob, new Short("1"));
        PageInfo<PersonalRelease> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 根据简历id删除
     */
    public Result delete(Long id) {
        if (id == null) {
            return Result.failure(ResultCode.FAILURE, "简历Id不能为空");
        }
        int k = personalReleaseMapper.deleteByPrimaryKey(id);
        if (k <= 0) {
            return Result.failure(ResultCode.FAILURE, "删除失败");
        }
        return Result.success();
    }

    /**
     * 根据简历id修改
     */
    public Result update(PersonalRelease record) {
        if (record.getId() == null) {
            return Result.failure(ResultCode.FAILURE, "简历Id不能为空");
        }
        if (Strings.isNullOrEmpty(record.getUpdateUser())) {
            return Result.failure(ResultCode.FAILURE, "修改人不能为空");
        }
        record.setUpdateUser(record.getUpdateUser());
        record.setUpdatetime(new Date());
        int i = personalReleaseMapper.updateByPrimaryKeySelective(record);
        if (i <= 0) {
            return Result.failure(ResultCode.FAILURE, "修改失败");
        }
        return Result.success();
    }

    /**
     * 查询发布简历列表
     */
    public Result queryPublishList(Integer pageNumber, Integer pageSize, String userNo) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户账号不能为空");
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<PersonalRelease> list = personalReleaseMapper.queryPublishList(userNo);
        if (list.size() > 0) {
        	for (int i = 0; i < list.size(); i++) {
            	getTypeName(list.get(i)); 
            }
		}
        PageInfo<PersonalRelease> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 查询发布简历列表
     */
    public Result selectByPrimaryKey(Long id, String userNo) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户账号不能为空");
        }
        if (id == null) {
            return Result.failure(ResultCode.FAILURE, "简历Id不能为空");
        }
        PersonalRelease personalRelease = personalReleaseMapper.selectByPrimaryKey(id);
        getTypeName(personalRelease);
        return Result.success(personalRelease);
    }

    /**
     * 聘请的人才列表
     */
    public Result queryEmployList(Integer pageNumber, Integer pageSize, String userNo) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户账号不能为空");
        }
        List<PersonalRelease> list = personalReleaseMapper.queryEmployList(userNo);
        PageInfo<PersonalRelease> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    
    public void getTypeName(PersonalRelease release) {
        DictEntry dictEntry = dictEntryMapper.get("DC_SEX", release.getSex());
        if (dictEntry != null) {
        	release.setSex(dictEntry.getDictname());
        }
        if (!Strings.isNullOrEmpty(release.getJobstatus())) {
        	DictEntry dictEntry1 = dictEntryMapper.get("DC_FINDJOB_STATUS", release.getSex());
            if (dictEntry1 != null) {
            	release.setSex(dictEntry1.getDictname());
            }
		}
        if (!Strings.isNullOrEmpty(release.getUserEdu())) {
        	DictEntry dictEntry2 = dictEntryMapper.get("DC_EDU", release.getUserEdu());
            if (dictEntry2 != null) {
            	release.setUserEdu(dictEntry2.getDictname());
            }
		}
        if (!Strings.isNullOrEmpty(release.getExprsalary())) {
        	DictEntry dictEntry3 = dictEntryMapper.get("DC_SALARY", release.getExprsalary());
            if (dictEntry3 != null) {
            	release.setExprsalary(dictEntry3.getDictname());
            }
		}
        if (!Strings.isNullOrEmpty(release.getAnnualsalary())) {
        	DictEntry dictEntry4 = dictEntryMapper.get("DC_SALARY", release.getAnnualsalary());
            if (dictEntry4 != null) {
            	release.setAnnualsalary(dictEntry4.getDictname());
            }
		}
        if (!Strings.isNullOrEmpty(release.getArea())) {
        	DictEntry dictEntry5 = dictEntryMapper.get("DC_NATIONALITY", release.getArea());
            if (dictEntry5 != null) {
            	release.setArea(dictEntry5.getDictname());
            }
		}
        if (!Strings.isNullOrEmpty(release.getMaritalstatus())) {
        	DictEntry dictEntry6 = dictEntryMapper.get("DC_MARRY_STATUS", release.getMaritalstatus());
            if (dictEntry6 != null) {
            	release.setMaritalstatus(dictEntry6.getDictname());
            }
		}
    }

}
